package main

import (
	"log"
	"github.com/streadway/amqp"
	"fmt"
)

func isError2(err error, msg string) {
	if err != nil{
		log.Fatal(fmt.Sprintf("%s: %s", msg, err))
		panic(fmt.Sprintf("%s:%s", msg, err))
	}
}

func main() {
	conn, err := amqp.Dial("amqp://localhost")
	isError2(err, "failed to connect to rabbitmq")
	defer conn.Close()

	c, err := conn.Channel()

	q, err := c.QueueDeclare("hello", true,false,false,false,nil)
	isError2(err, "failed to created queue")

	deliver, err := c.Consume(
		q.Name,
		"",
		false,
		false,
		false,
		false,
		nil,
	)



	for d := range deliver {
		log.Printf("Received a message: %s", d.Body)
		//dot_count := bytes.Count(d.Body, []byte("."))
		//t := time.Duration(dot_count)
		//time.Sleep(t * time.Second)
		//log.Printf("Done")

		// 确认消息被收到！！如果为真的，那么同在一个channel,在该消息之前未确认的消息都会确认，适合批量处理
		// 真时场景：每十条消息确认一次，类似
		d.Ack(true)
	}

}